﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="CategoriesListDialog.cs" company="YellowFish">
//   Copyright © YellowFish 2009
// </copyright>
// <summary>
//   Describes and manipulates the categories list dialog.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace MobilePal.Dialogs
{
  #region Imported Namespaces


  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Windows.Forms;

  using State;


  #endregion


  /// <summary>
  /// Describes and manipulates the categories list dialog.
  /// </summary>
  public partial class CategoriesListDialog: ListDialogBase
  {
    #region Constants and Fields


    private readonly ActivityCategoryCollection _categoryCollection;


    #endregion


    #region Constructors and Destructors


    /// <summary>
    /// Initializes a new instance of the <see cref="CategoriesListDialog"/> class.
    /// </summary>
    /// <param name="categoryCollection">
    /// The category collection.
    /// </param>
    public CategoriesListDialog(ActivityCategoryCollection categoryCollection)
    {
      _categoryCollection = categoryCollection;

      InitializeComponent();

      ListView = categoriesList;
      UpdateCloseMenuItem();
    }


    #endregion


    #region Methods


    /// <summary>
    /// Gets the list view item.
    /// </summary>
    /// <param name="item">
    /// The item.
    /// </param>
    /// <returns>
    /// </returns>
    protected override string GetListViewItemText(object item)
    {
      return ((ActivityCategory)item).Name;
    }


    /// <summary>
    /// Gets the sorted items list.
    /// </summary>
    /// <returns>
    /// </returns>
    protected override List<object> GetSortedItemsList()
    {
      var categories = new List<ActivityCategory>(_categoryCollection);

      categories.Sort((category1, category2) => string.Compare(category1.Name, category2.Name));

      return new List<object>(categories.Cast<object>());
    }


    /// <summary>
    /// Called when show check boxes flag changed.
    /// </summary>
    protected override void OnShowCheckBoxesChanged()
    {
      UpdateCloseMenuItem();
    }


    /// <summary>
    /// Updates the controls.
    /// </summary>
    protected override void UpdateControls()
    {
      UpdateControls(true);
    }


    private void AddButtonClick(object sender, EventArgs e)
    {
      var category = new ActivityCategory();
      category.Name = nameTextBox.Text;

      _categoryCollection.Add(category);
      InsertItem(category);
    }


    private void CloseMenuItemClick(object sender, EventArgs e)
    {
      DialogResult = ShowCheckBoxes ? DialogResult.Cancel : DialogResult.OK;

      Close();
    }


    private void NameTextBoxTextChanged(object sender, EventArgs e)
    {
      UpdateControls(false);
    }


    private void RemoveMenuItemClick(object sender, EventArgs e)
    {
      ListViewItem item = FocusedListViewItem;
      DialogResult result = MessageBox.Show("Are you sure you want to remove category '" + item.Text + "'?", 
                                            "Remove Category?", MessageBoxButtons.YesNo, MessageBoxIcon.Question, 
                                            MessageBoxDefaultButton.Button2);

      if (result != DialogResult.Yes)
      {
        return;
      }

      _categoryCollection.Remove((ActivityCategory)item.Tag);
      RemoveItem(item);
    }


    private void UpdateButtonClick(object sender, EventArgs e)
    {
      string text = nameTextBox.Text;

      FocusedListViewItem.Text = text;
      UpdateColumnsWidths();

      var category = (ActivityCategory)FocusedListViewItem.Tag;
      category.Name = text;

      UpdateControls();
    }


    private void UpdateCloseMenuItem()
    {
      closeMenuItem.Text = ShowCheckBoxes ? "Cancel" : "Close";
    }


    private void UpdateControls(bool updateTextBox)
    {
      if (updateTextBox)
      {
        UpdateTextBox();
      }

      string text = nameTextBox.Text;

      addButton.Enabled = !_categoryCollection.Contains(text) && !string.IsNullOrEmpty(text);
      updateButton.Enabled = !_categoryCollection.Contains(text) && !string.IsNullOrEmpty(text)
                             && FocusedListViewItem != null;

      removeMenuItem.Enabled = FocusedListViewItem != null && FocusedItem != _categoryCollection.DefaultCategory;
    }


    private void UpdateTextBox()
    {
      ListViewItem item = FocusedListViewItem;

      if (item == null)
      {
        nameTextBox.Text = string.Empty;
        return;
      }

      var category = (ActivityCategory)item.Tag;

      nameTextBox.Text = category.Name;
    }


    #endregion
  }
}